package com.example.multiaccountfetcher.strategy.impl;

import com.example.multiaccountfetcher.model.RequestParams;
import com.example.multiaccountfetcher.strategy.DataFetchStrategy;
import org.springframework.stereotype.Component;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 模拟第一个API接口的数据拉取策略
 */
@Component
public class MockApiOneFetchStrategy implements DataFetchStrategy {
    
    // 最大拉取次数
    private static final int MAX_FETCH_COUNT = 5;
    
    // 请求间隔时间（秒）
    private static final int REQUEST_INTERVAL_SECONDS = 3;
    
    @Override
    public CompletableFuture<Object> fetchData(String accountId, Object params) {
        return CompletableFuture.supplyAsync(() -> {
            try {
                // 模拟网络请求延迟
                int delay = ThreadLocalRandom.current().nextInt(500, 2000);
                Thread.sleep(delay);
                
                // 处理特定的参数
                String paramDetails = "";
                if (params instanceof RequestParams) {
                    RequestParams requestParams = (RequestParams) params;
                    paramDetails = String.format(", dataType: %s, startTime: %s, endTime: %s",
                            requestParams.getDataType(),
                            requestParams.getStartTime(),
                            requestParams.getEndTime());
                }
                
                // 模拟返回数据
                return "Data from API_ONE for account: " + accountId + paramDetails + ", delay: " + delay + "ms";
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new RuntimeException("Fetch data interrupted", e);
            }
        });
    }
    
    @Override
    public boolean shouldContinueFetching(String accountId, int currentCount) {
        // 简单策略：每个账号最多拉取MAX_FETCH_COUNT次
        return currentCount < MAX_FETCH_COUNT;
    }
    
    @Override
    public String getInterfaceType() {
        return "API_ONE";
    }
    
    @Override
    public int getRequestIntervalSeconds() {
        return REQUEST_INTERVAL_SECONDS;
    }
}